#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author:    御风
# @Mail:      zhong.yufeng@foxmail.com
# @Time:      2022/4/9 4:40


import re
import random
import hashlib


def strRandom(strLen: int, baseStr: str = "0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ"):
    return "".join([random.choice(baseStr) for _ in range(strLen)])


def strFullToHalf(fullString: str):
    """
    全角转半角
    """
    half = []
    for full in fullString:
        s = ""
        for uchar in full:
            inside = ord(uchar)
            if inside == 0x3000:
                inside = 0x0020
            elif 0xFF01 <= inside <= 0xFF5E:
                inside -= 0xFEE0
            s += chr(inside)
        half.append(s)
    return "".join(half)


def strHalfToFull(halfString: str):
    """
    半角转全角
    """
    full = []
    for half in halfString:
        s = ""
        for uchar in half:
            inside = ord(uchar)
            if inside == 0x0020:
                inside = 0x3000
            elif 0x0020 < inside < 0x007F:
                inside += 0xFEE0
            s += chr(inside)
        full.append(s)
    return "".join(full)


def strToHump(text: str):
    if isinstance(text, str):
        matchs = re.findall(r"_+\w", text)
        if matchs:
            for m in matchs:
                text = text.replace(m, m.strip("_").upper())
        if text[0].isupper():
            text = text[0].lower() + text[1:]
    return text


def md5(text: str, encode="utf-8"):
    data = text.encode(encode)
    hasher = hashlib.md5()
    hasher.update(data)
    return hasher.hexdigest()
